#include <iostream>
#include <cstring>
using namespace std;

int price[] = {0, 0, 0, 0, 0, 0, 10000, 36, 720, 360, 80, 252, 108, 72, 54, 180, 72, 180, 119, 36, 306, 1080, 144, 1800, 3600};
int field[4][4];
int flag[10];

int main()
{
    int x, y;
    memset(flag, -1, sizeof(flag));
    for (int i = 1; i <= 3; i++)
    {
        for (int j = 1; j <= 3; j++)
        {
            cin >> field[i][j];
            flag[field[i][j]] = 1;
            if (field[i][j] == 0)
            {
                x = i;
                y = j;
            }
        }
    }
    for (int i = 0; i < 10; i++)
    {
        if (flag[i] == -1)
        {
            field[x][y] = i;
        }
    }
    for (int i = 0; i < 3; i++)
    {
        cin >> x >> y;
        cout << field[x][y] << endl;
    }
    int input;
    cin >> input;
    int sum = 0;
    switch (input)
    {
    case 1:
        sum += (field[1][1] + field[1][2] + field[1][3]);
        break;
    case 2:
        sum += (field[2][1] + field[2][2] + field[2][3]);
        break;
    case 3:
        sum += (field[3][1] + field[3][2] + field[3][3]);
        break;
    case 4:
        sum += (field[1][1] + field[2][1] + field[3][1]);
        break;
    case 5:
        sum += (field[1][2] + field[2][2] + field[3][2]);
        break;
    case 6:
        sum += (field[1][3] + field[2][3] + field[3][3]);
        break;
    case 7:
        sum += (field[1][1] + field[2][2] + field[3][3]);
        break;
    case 8:
        sum += (field[1][3] + field[2][2] + field[3][1]);
        break;
    }
    cout << price[sum];
    return 0;
}